from django.shortcuts import render

# Create your views here.
from rest_framework.viewsets import ReadOnlyModelViewSet
from .serializers import AreaSerializer,SubAreaSerializer
from rest_framework_extensions.cache.mixins import RetrieveCacheResponseMixin,ListCacheResponseMixin,CacheResponseMixin


from .models import Area


class AreaView(CacheResponseMixin,ReadOnlyModelViewSet):
    """
    # 传统的serializer_class 方法 不能满足 需求
    # 需要重写get_serializer 方法
    设置queryset 将所有的信息都返回了
    需要重写get_queryset方法
    省份信息:select * from tb_areas where parend_id is null;
    市或区县:select * from tb_areas where parend_id=13000;

    """

    def get_queryset(self):

        if self.action == 'list':
    #         返回省份信息
            return Area.objects.filter(parent__isnull=True)

        else:
            return Area.objects.all()






    def get_serializer_class(self):

        if self.action == "list":

            return AreaSerializer

        else:
            return SubAreaSerializer
